<html>
	<?php
		require_once("classes/User.php");
		require_once("classes/Database.php");
		require_once("classes/Head.php");
		require_once("classes/Section.php");
		require_once("classes/Image.php");
		require_once("classes/Title.php");
		require_once("classes/Paragraph.php");
		require_once("classes/LinkList.php");
		require_once("classes/EntryList.php");
		
		$head = new Head();
		
		if(!isset($_SESSION["login"])){
			session_start();
		}
		
		$user = $_SESSION["user"];
		if($_SESSION["login"]){
			$title = "Bem-Vindo, " . $user->getName();
		}else{
			$title = "Login Errado";
		}
		$head->addLayout("css/style.css");
		$head->addTitle($title);
		
		$head->printHead();
	?>
	<body id="body">
		<div id="struture">
			<div id="container">
		        <?php
		        
		        	$header = new Section("header");					
						$header->addElement(new Image("images/job.png", "logo", "100px", "100px"));
						
						$title = new Title("Ofertas de Emprego", "title");
						$header->addElement($title);
					$header->printSection();
		        	
		        	/* Left Page */
		        	$leftPage = new Section("leftPage");
					if($_SESSION["login"]){
						$text = "Bem-vindo, " . $user->getName() . ".";
						if($user->isThisRole("candidate")){
							$leftPage->addElement(new Title("Candidato", "menuTitle"));
							
							$leftPage->addElement(new Paragraph($text, "menu"));
							$logout = new LinkList("menuEntry");
							$logout->addLink("Logout", "scripts/logout.php", new Image("images/leave.png", "icon", "16px", "16px"));
							$leftPage->addElement($logout);
							
							$leftPage->addElement(new Title("Links Uteis", "menuTitle"));
							
							$links = new LinkList("menuEntry");
							$links->addLink("Criar/Modificar C.V", "createCV.php", new Image("images/add.png", "icon", "16px", "16px"));
							$links->addLink("Ver C.V", "viewCV.php", new Image("images/view.png", "icon", "16px", "16px"));
							$links->addLink("Pesquisar Anuncios", "findAnnouncement.php", new Image("images/search.png", "icon", "16px", "16px"));
							$links->addLink("Pesquisar Empresas", "findEnterprise.php", new Image("images/search.png", "icon", "16px", "16px"));
							$links->addLink("Meus Posts", "findPosts.php", new Image("images/my.png", "icon", "16px", "16px"));
							$leftPage->addElement($links);
						}else if($user->isThisRole("enterprise")){
							$leftPage->addElement(new Title("Empresa", "menuTitle"));
							
							$leftPage->addElement(new Paragraph($text, "menu"));
							$logout = new LinkList("menuEntry");
							$logout->addLink("Logout", "scripts/logout.php", new Image("images/leave.png", "icon", "16px", "16px"));
							$leftPage->addElement($logout);
							
							$leftPage->addElement(new Title("Links Uteis", "menuTitle"));
							
							$links = new LinkList("menuEntry");
							$links->addLink("Criar Anuncio", "createAnnouncement.php", new Image("images/add.png", "icon", "16px", "16px"));
							$links->addLink("Meus Anuncios", "scripts/searchAnnouncement.php?enterprise=".$user->getId(), new Image("images/my.png", "icon", "16px", "16px"));
							$links->addLink("Pesquisar C.Vs", "findCV.php", new Image("images/search.png", "icon", "16px", "16px"));
							$links->addLink("Pesquisar Candidatos", "findCandidate.php", new Image("images/search.png", "icon", "16px", "16px"));
							$leftPage->addElement($links);
						}else if($user->isThisRole("admin")){
							$leftPage->addElement(new Title("Admin", "menuTitle"));
							$leftPage->addElement(new Paragraph($text, "menu"));
							$logout = new LinkList("menuEntry");
							$logout->addLink("Logout", "scripts/logout.php", new Image("images/leave.png", "icon", "16px", "16px"));
							$leftPage->addElement($logout);
						}
						
						if($user->isThisRole("admin")){
							$leftPage->addElement(new Title("Administra&ccedil&atildeo", "menuTitle"));
							
							$adminLinks = new LinkList("menuEntry");
							$adminLinks->addLink("Gerir Anuncios", "#", new Image("images/ad_tool.png", "icon", "16px", "16px"));
							$adminLinks->addLink("Gerir C.Vs", "#", new Image("images/doc_tool.png", "icon", "16px", "16px"));
							$adminLinks->addLink("Gerir Candidatos", "#", new Image("images/user_tool.png", "icon", "16px", "16px"));
							$adminLinks->addLink("Gerir Empresas", "#", new Image("images/emp_tool.png", "icon", "16px", "16px"));
							$leftPage->addElement($adminLinks);
						}
						
						$leftPage->printSection();
						$content = new Section("content");
						$content->addElement(new Title("Ultimos Anuncios", "seccaoTitle"));
						$db = new Database("localhost", "root", "");
						$db->connect("emprego");
						$db->executeQuery("SELECT a.title, a.id, u.nome as enterprise, c.name as cat, l.name as loc, a.savedate
											FROM announcement a, users u, category c, location l
											WHERE a.owner = u.id AND a.category = c.id AND a.location = l.id
											ORDER BY savedate DESC LIMIT 10");
						
						$entry = new EntryList("entry");
						while($line = $db->getResultLine(MYSQL_ASSOC)){
							$entry->openEntry();
							$entry->addLinkTitle("announcement.php?ad=".$line["id"], $line["title"]);
							$entry->addEntry("Empresa:", $line["enterprise"]);
							$entry->addEntry("Categoria:", $line["cat"]);
							$entry->addEntry("Localiza&ccedil&atildeo:", $line["loc"]);
							$entry->addEntry("Data:", $line["savedate"]);
							$entry->closeEntry();
							$entry->addHtmlCode("</br></br>");
						}
						$content->addElement($entry);
						$content->printSection();
					}else{
						$text = "Login Errado.";
						$leftPage->addElement(new Title("ERRO", "menuTitle"));
						$leftPage->addElement(new Paragraph($text, "menu"));
						
						$leave = new LinkList("menuEntry");
						$leave->addLink("Voltar", "index.php", new Image("images/leave.png", "icon", "16px", "16px"));
						$leftPage->addElement($leave);
						$leftPage->printSection();
						
						$content = new Section("content");
						$content->addElement(new Title("Erro", "seccaoTitle"));
						
						$content->printSection();
					}
					
					$footer = new Section("footer");
						$text = new Paragraph("Copyright&copy Nuno Pombo", "");
						$footer->addElement($text);
					$footer->printSection();
		        ?>
			</div>
		</div>
	</body>
</html>